﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Tetris
{
    abstract class Shape 
    {

        protected bool _fallenShape = false;
        protected List<Point> _coordinates;
        protected List<Point> _lastcoordinates;
        protected int Pos = 0;

        public bool FallenShape
        {
            get { return _fallenShape; }
        }
        public List<Point> LastCoordinates
        {
            get { return _lastcoordinates; }
        }
        public List<Point> Coordinates
        {
            get { return _coordinates; }
        }

        public Shape(Point _first, Point _second, Point _third, Point _fourth)
        {
            _coordinates = new List<Point>();
            _coordinates.Add(_first);
            _coordinates.Add(_second);
            _coordinates.Add(_third);
            _coordinates.Add(_fourth);
            _lastcoordinates = new List<Point>();
            Point _first2 = new Point(0, 0);
            Point _second2 = new Point(0, 0);
            Point _third2 = new Point(0, 0);
            Point _fourth2 = new Point(0, 0);
            _lastcoordinates.Add(_first2);
            _lastcoordinates.Add(_second2);
            _lastcoordinates.Add(_third2);
            _lastcoordinates.Add(_fourth2);
        }
        public void RemovePoint(int x, int y)
        {
                for (int i = 0; i <= _coordinates.Count - 1; i++)
                {
                    if (_coordinates[i].x == x && _coordinates[i].y == y)
                    {
                        _coordinates.RemoveAt(i);
                    }
                }
        }
        public void Fall(Func<int, int, int> IsPosFree, Action<Shape> IsGameOv, Action<Shape> SaveShape)
        {
            
            for (int i = 0; i <= _coordinates.Count - 1; i++)
            {
                _lastcoordinates[i].x = _coordinates[i].x;
                _lastcoordinates[i].y = _coordinates[i].y;
            }
                foreach (Point item in _coordinates)
                {
                    if (IsPosFree(item.x, item.y + 1) != 0)
                    {
                        _fallenShape = true;
                        IsGameOv(this);
                        SaveShape(this);
                    }
                }
            if (_fallenShape != true)
            {
                foreach (Point item in _coordinates)
                {
                   item.PlusY();        
                }
            }
        }
        public void MoveLeft(Func<int, int, int> IsPosFree)
        {
            for (int i = 0; i <= _coordinates.Count - 1; i++)
            {
                _lastcoordinates[i].x = _coordinates[i].x;
                _lastcoordinates[i].y = _coordinates[i].y;
            }
            foreach (Point item in _coordinates)
            {

                if (IsPosFree(item.x - 1, item.y) != 0)
                {
                    return;
                }
                else
                {
                    foreach (Point item2 in _coordinates)
                    {

                        item2.MinX();

                    }
                }
            }
            
        }
        public void MoveRight(Func<int, int, int> IsPosFree)
        {
            for (int i = 0; i <= _coordinates.Count - 1; i++)
            {
                _lastcoordinates[i].x = _coordinates[i].x;
                _lastcoordinates[i].y = _coordinates[i].y;
            }
            foreach (Point item in _coordinates)
            {

                if (IsPosFree(item.x + 1, item.y) != 0)
                {
                    return;
                }
                else
                {
                    foreach (Point item2 in _coordinates)
                    {

                        item2.PlusX();

                    }
                }
            }

        }
        public void MoveDown()
        { 
        }
        abstract public void Rotate(Func<int, int, int> IsPosFree);
        
    }
}
